import type { Consumer } from './model';

import type { ID } from '#/api/common';
import type { Page } from '#/interface/page';

import { requestClient } from '#/api/request';

enum Api {
  addConsumer = '/iot/consumer/add',
  consumerClient = '/iot/server/client/list',
  consumerDetail = '/iot/consumer/detail',
  consumerList = '/iot/consumer/page',
  delConsumer = '/iot/consumer/remove',
  subscribeList = '/iot/consumer/subscribeList',
  subscribeselect = '/iot/server/subscribe/select',
  unsubscription = '/iot/consumer/unsubscription',
  updateConsumer = '/iot/consumer/update',
}

/**
 * 新增
 * @param data 参数
 */
export function addConsumer(data: Partial<Consumer>) {
  return requestClient.postWithMsg<void>(Api.addConsumer, data);
}

/**
 * 列表
 * @returns list
 */
export function consumerList(params?: { pageNum: number; pageSize: number }) {
  return requestClient.get<Page<Consumer>>(Api.consumerList, { params });
}

/**
 * 删除
 * @param id
 */
export function delConsumer(id: ID) {
  return requestClient.postWithMsg<void>(`${Api.delConsumer}?id=${id}`);
}

/**
 * 修改
 * @param data
 */
export function updateConsumer(data: Partial<Consumer>) {
  return requestClient.postWithMsg<void>(Api.updateConsumer, data);
}

/**
 * 详情
 * @returns list
 */
export function consumerDetail(id: ID) {
  return requestClient.get<Consumer>(`${Api.consumerDetail}?id=${id}`);
}

/**
 * 订阅列表
 * @returns list
 */
export function subscribeList(id: ID) {
  return requestClient.get<Consumer>(`${Api.subscribeList}?consumerId=${id}`);
}

/**
 * 解除订阅
 * @param consumerId serverSubscribeId
 */
export function unsubscription(consumerId: ID, serverSubscribeId: ID) {
  return requestClient.postWithMsg<void>(
    `${Api.unsubscription}?consumerId=${consumerId}&serverSubscribeId=${serverSubscribeId}`,
  );
}

/**
 * 客户端
 * @returns list
 */
export function consumerClient(id: ID) {
  return requestClient.get<Consumer>(`${Api.consumerClient}?groupID=${id}`);
}
/**
 * 订阅下拉数据源
 * @returns list
 */
export function subscribeselect() {
  return requestClient.get<Consumer>(`${Api.subscribeselect}`);
}
